SlideShare a Scribd company logo
Class No.38  Data Structures http://ecomputernotes.com
Sorting http://ecomputernotes.com
Sorting Integers 20 8 5 10 7 5 7 8 10 20 How to sort the integers in this array? http://ecomputernotes.com
Elementary Sorting Algorithms Selection Sort Insertion Sort Bubble Sort http://ecomputernotes.com
Selection Sort  Main idea: find the smallest element  put it in the first position find the next smallest element put it in the second position … And so on, until you get to the end of the list http://ecomputernotes.com
Selection Sort -- Example 1 2 3 0 7 12 19 5 a: 19 1 2 3 0 7 19 12 5 a: 12 1 2 3 0 19 7 12 5 a: 7 1 2 3 0 7 12 19 5 a: 1 2 3 0 5 7 12 19 a: a: 1 2 3 0 5 http://ecomputernotes.com
Selection Sort: Code void selectionSort(int *arr, int N) { int posmin, count, tmp; for(count=0;count<N;count++) {  posmin = findIndexMin(arr,count,N); tmp=arr[posmin]; arr[posmin]=arr[count]; arr[count]=tmp; } } http://ecomputernotes.com
Selection Sort: Code int findIndexMin(int *arr, int start, int N) { int posmin=start; int index; for(index=start; index < N; index++) if (arr[index]<arr[posmin]) posmin=index; return posmin; } http://ecomputernotes.com
Swap Action (SelectionSorting) 5 8 20 10 7 5 7 20 10 8 5 7 8 10 20 20 8 5 10 7 5 7 8 10 20 http://ecomputernotes.com
Selection Sort Analysis What is the time complexity of this algorithm? Worst case == Best case == Average case  Each iteration performs a linear search on the rest of the array first element N + second element N-1 +  … penultimate element 2 + last element 1 Total N(N+1)/2  = (N 2 +N)/2 http://ecomputernotes.com
Insertion Sort  Basic idea ( sorting cards ): Starts by considering the first two elements of the array data, if out of order, swap them Consider the third element, insert it into the proper position among the first three elements. Consider the forth element, insert it into the proper position among the first four elements. … …  http://ecomputernotes.com
Insertion Sort -- Example 1 2 3 0 12 5 7 19 a : 1 2 3 0 19 5 7 12 a : 1 2 3 0 12 19 7 5 a : 1 2 3 0 7 12 19 5 a : http://ecomputernotes.com
Insertion Sort: Code void insertionSort(int *arr, int N) { int pos, count, val; for(count=1; count < N; count++) {  val = arr[count]; for(pos=count-1; pos >= 0; pos--)  if (arr[pos] > val) arr[pos+1]=arr[pos]; else break; arr[pos+1] = val; } } http://ecomputernotes.com
Insertion Sort -- animation 1 2 3 0 12 5 7 19 a : 1 2 3 0 19 5 7 19 a : count val pos 1 12 0 1 12 -1 1 2 3 0 5 7 19 a : 12 19 1 2 3 0 19 5 7 12 a : 12 http://ecomputernotes.com
Insertion Sort -- animation (cont) 1 2 3 0 12 19 7 5 a : count val pos 2 5 1 2 5 -1 1 2 3 0 19 5 7 12 a : 1 2 3 0 5 7 12 a : 19 19 1 2 3 0 19 7 12 a : 19 12 2 5 0 1 2 3 0 12 19 7 12 a : 5 http://ecomputernotes.com
Insertion Sort -- animation (cont) 1 2 3 0 12 19 7 5 a : count val pos 3 7 2 3 7 0 3 7 1 1 2 3 0 12 19 7 5 a : 19 1 2 3 0 12 19 19 5 a : 12 1 2 3 0 12 12 19 5 a : 7 1 2 3 0 7 12 19 5 a : http://ecomputernotes.com
Insertion Sort Analysis  What is the time complexity of this algorithm?  Worst case > Average case > Best case Each iteration inserts an element at the start of the array, shifting all sorted elements along second element 2 + … penultimate element N-1 + last element N Total (2+N)(N-1)/2 = O(N 2 ) http://ecomputernotes.com
Bubble Sort Basic idea ( lighter bubbles rise to the top ): Exchange neighbouring items until the largest item reaches the end of the array Repeat for the rest of the array http://ecomputernotes.com
Bubble Sort -- Example 1 2 3 0 12 7 19 5 a: 1 2 3 0 12 7 19 5 a: 19 12 7 5 1 2 3 0 a: 12 19 7 5 a: 1 2 3 0 5 12 7 19 1 2 3 0 a: a: 1 2 3 0 12 7 19 5 a: 1 2 3 0 7 12 19 5 a: 1 2 3 0 7 12 19 5 a: 1 2 3 0 7 12 19 5 a: 1 2 3 0 7 12 19 5 a: http://ecomputernotes.com
Bubble Sort: Code void bubbleSort(int *arr, int N){ int i, temp, bound = N-1; int swapped = 1; while (swapped > 0 ) { swapped = 0; for(i=0; I < bound; i++) if ( arr[i] > arr[i+1] ) { temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; swapped = i; } bound = swapped; } } http://ecomputernotes.com
Bubble Sort Analysis  What is the time complexity of this algorithm?  Worst case > Average case > Best case Each iteration compares all the adjacent elements, swapping them if necessary first iteration N + second iteration N-1 + … last iteration 1 Total N(1+N)/2 = O(N 2 ) http://ecomputernotes.com
Summary  Insertion, Selection and Bubble sort:  Worst case time complexity is proportional to N 2 .  Best   sorting routines are N log(N)  http://ecomputernotes.com
NLogN Algorithms Divide and Conquer Merge Sort Quick Sort Heap Sort http://ecomputernotes.com
Divide and Conquer What if we split the list into two parts? 10 12 8 4 2 11 7 5 10 12 8 4 2 11 7 5 http://ecomputernotes.com
Divide and Conquer Sort the two parts: 10 12 8 4 2 11 7 5 4 8 10 12 2 5 7 11 http://ecomputernotes.com
Divide and Conquer Then merge the two parts together: 4 8 10 12 2 5 7 11 2 4 5 7 8 10 11 12 http://ecomputernotes.com

More Related Content

PPT
Computer notes - Sorting
PDF
Runtime Monitoring of Stream Logic Formulae (Talk @ FPS 2015)
PDF
MapReduce for Parallel Trace Validation of LTL Properties
PDF
PPTX
Queues presentation
PPTX
Queues in C++
PPT
QUEUE IN DATA STRUCTURE USING C
Computer notes - Sorting
Runtime Monitoring of Stream Logic Formulae (Talk @ FPS 2015)
MapReduce for Parallel Trace Validation of LTL Properties
Queues presentation
Queues in C++
QUEUE IN DATA STRUCTURE USING C

What's hot (20)

PPT
computer notes - Data Structures - 9
PPSX
PDF
05 queues
PPT
Stacks, Queues, Deques
PPTX
Queue - Data Structure - Notes
PPTX
Queue
PPTX
Queue Implementation Using Array & Linked List
PPT
Stack and queue
PPT
Queue data structure
PPT
Queue implementation
PPTX
My lectures circular queue
PPTX
Stack - Data Structure - Notes
PPTX
Unit 4 queue
PPTX
Queue in Data Structure
PPT
Computer notes - Hashing
PPT
03 stacks and_queues_using_arrays
PPTX
Application of Stack - Yadraj Meena
PPTX
STACKS IN DATASTRUCTURE
PPT
stack and queue array implementation in java.
computer notes - Data Structures - 9
05 queues
Stacks, Queues, Deques
Queue - Data Structure - Notes
Queue
Queue Implementation Using Array & Linked List
Stack and queue
Queue data structure
Queue implementation
My lectures circular queue
Stack - Data Structure - Notes
Unit 4 queue
Queue in Data Structure
Computer notes - Hashing
03 stacks and_queues_using_arrays
Application of Stack - Yadraj Meena
STACKS IN DATASTRUCTURE
stack and queue array implementation in java.
Ad

Viewers also liked (20)

PPT
computer notes - Data Structures - 12
PPT
computer notes - Data Structures - 6
PPT
computer notes - Data Structures - 26
PPT
computer notes - Data Structures - 31
PPT
computer notes - Data Structures - 5
PPT
computer notes - Data Structures - 8
PPT
computer notes - Data Structures - 32
PPT
computer notes - Data Structures - 7
PPT
computer notes - Data Structures - 14
PPT
computer notes - Data Structures - 1
PPT
computer notes - Data Structures - 18
PPT
computer notes - Data Structures - 2
PPT
computer notes - Data Structures - 25
PPT
computer notes - Data Structures - 19
PPT
computer notes - Data Structures - 33
PDF
computer notes - Deleting a node
PPT
computer notes - Data Structures - 28
PPT
computer notes - Data Structures - 27
PPT
computer notes - Data Structures - 35
PPT
computer notes - Data Structures - 34
computer notes - Data Structures - 12
computer notes - Data Structures - 6
computer notes - Data Structures - 26
computer notes - Data Structures - 31
computer notes - Data Structures - 5
computer notes - Data Structures - 8
computer notes - Data Structures - 32
computer notes - Data Structures - 7
computer notes - Data Structures - 14
computer notes - Data Structures - 1
computer notes - Data Structures - 18
computer notes - Data Structures - 2
computer notes - Data Structures - 25
computer notes - Data Structures - 19
computer notes - Data Structures - 33
computer notes - Deleting a node
computer notes - Data Structures - 28
computer notes - Data Structures - 27
computer notes - Data Structures - 35
computer notes - Data Structures - 34
Ad

Similar to computer notes - Data Structures - 38 (20)

PPT
computer notes - Data Structures - 39
PPT
Computer notes - Mergesort
PPT
Insersion & Bubble Sort in Algoritm
PPT
Computer notes data structures - 9
PPT
Advanced s and s algorithm.ppt
PPTX
sorting.pptx
PPT
14-sorting.ppt
PPT
14-sorting (3).ppt
PPT
14-sorting.ppt
PPT
14-sorting.ppt
PPTX
Sorting pnk
PPTX
algorithm assignmenteeeeeee.pptx
PPTX
Algorithim lec1.pptx
PPTX
Chapter 8 Sorting in the context of DSA.pptx
PDF
Chapter 8 advanced sorting and hashing for print
PPT
Sorting Algorithms
PPTX
Basic Sorting algorithms csharp
PDF
Introduction to Erlang
PPT
Data Structures 6
PPTX
Introduction to Algorithms
computer notes - Data Structures - 39
Computer notes - Mergesort
Insersion & Bubble Sort in Algoritm
Computer notes data structures - 9
Advanced s and s algorithm.ppt
sorting.pptx
14-sorting.ppt
14-sorting (3).ppt
14-sorting.ppt
14-sorting.ppt
Sorting pnk
algorithm assignmenteeeeeee.pptx
Algorithim lec1.pptx
Chapter 8 Sorting in the context of DSA.pptx
Chapter 8 advanced sorting and hashing for print
Sorting Algorithms
Basic Sorting algorithms csharp
Introduction to Erlang
Data Structures 6
Introduction to Algorithms

More from ecomputernotes (20)

PPT
computer notes - Data Structures - 30
PPT
computer notes - Data Structures - 11
PPT
computer notes - Data Structures - 20
PPT
computer notes - Data Structures - 15
DOC
Computer notes - Including Constraints
DOC
Computer notes - Date time Functions
DOC
Computer notes - Subqueries
DOC
Computer notes - Other Database Objects
PPT
computer notes - Data Structures - 4
PPT
computer notes - Data Structures - 13
DOC
Computer notes - Advanced Subqueries
DOC
Computer notes - Aggregating Data Using Group Functions
PPT
computer notes - Data Structures - 16
PPT
computer notes - Data Structures - 22
PPT
computer notes - Data Structures - 36
DOC
Computer notes - Enhancements to the GROUP BY Clause
DOC
Computer notes - Manipulating Data
DOC
Computer notes - Writing Basic SQL SELECT Statements
PPT
computer notes - Data Structures - 10
DOC
Computer notes - Controlling User Access
computer notes - Data Structures - 30
computer notes - Data Structures - 11
computer notes - Data Structures - 20
computer notes - Data Structures - 15
Computer notes - Including Constraints
Computer notes - Date time Functions
Computer notes - Subqueries
Computer notes - Other Database Objects
computer notes - Data Structures - 4
computer notes - Data Structures - 13
Computer notes - Advanced Subqueries
Computer notes - Aggregating Data Using Group Functions
computer notes - Data Structures - 16
computer notes - Data Structures - 22
computer notes - Data Structures - 36
Computer notes - Enhancements to the GROUP BY Clause
Computer notes - Manipulating Data
Computer notes - Writing Basic SQL SELECT Statements
computer notes - Data Structures - 10
Computer notes - Controlling User Access

Recently uploaded (20)

PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Electronic commerce courselecture one. Pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPT
Teaching material agriculture food technology
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
Big Data Technologies - Introduction.pptx
PDF
Empathic Computing: Creating Shared Understanding
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Spectroscopy.pptx food analysis technology
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
Understanding_Digital_Forensics_Presentation.pptx
Unlocking AI with Model Context Protocol (MCP)
Spectral efficient network and resource selection model in 5G networks
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Electronic commerce courselecture one. Pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Dropbox Q2 2025 Financial Results & Investor Presentation
“AI and Expert System Decision Support & Business Intelligence Systems”
NewMind AI Weekly Chronicles - August'25 Week I
Teaching material agriculture food technology
Diabetes mellitus diagnosis method based random forest with bat algorithm
The Rise and Fall of 3GPP – Time for a Sabbatical?
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Big Data Technologies - Introduction.pptx
Empathic Computing: Creating Shared Understanding
Advanced methodologies resolving dimensionality complications for autism neur...
Spectroscopy.pptx food analysis technology
20250228 LYD VKU AI Blended-Learning.pptx
MIND Revenue Release Quarter 2 2025 Press Release
Understanding_Digital_Forensics_Presentation.pptx

computer notes - Data Structures - 38

  • 1. Class No.38 Data Structures http://ecomputernotes.com
  • 3. Sorting Integers 20 8 5 10 7 5 7 8 10 20 How to sort the integers in this array? http://ecomputernotes.com
  • 4. Elementary Sorting Algorithms Selection Sort Insertion Sort Bubble Sort http://ecomputernotes.com
  • 5. Selection Sort Main idea: find the smallest element put it in the first position find the next smallest element put it in the second position … And so on, until you get to the end of the list http://ecomputernotes.com
  • 6. Selection Sort -- Example 1 2 3 0 7 12 19 5 a: 19 1 2 3 0 7 19 12 5 a: 12 1 2 3 0 19 7 12 5 a: 7 1 2 3 0 7 12 19 5 a: 1 2 3 0 5 7 12 19 a: a: 1 2 3 0 5 http://ecomputernotes.com
  • 7. Selection Sort: Code void selectionSort(int *arr, int N) { int posmin, count, tmp; for(count=0;count<N;count++) { posmin = findIndexMin(arr,count,N); tmp=arr[posmin]; arr[posmin]=arr[count]; arr[count]=tmp; } } http://ecomputernotes.com
  • 8. Selection Sort: Code int findIndexMin(int *arr, int start, int N) { int posmin=start; int index; for(index=start; index < N; index++) if (arr[index]<arr[posmin]) posmin=index; return posmin; } http://ecomputernotes.com
  • 9. Swap Action (SelectionSorting) 5 8 20 10 7 5 7 20 10 8 5 7 8 10 20 20 8 5 10 7 5 7 8 10 20 http://ecomputernotes.com
  • 10. Selection Sort Analysis What is the time complexity of this algorithm? Worst case == Best case == Average case Each iteration performs a linear search on the rest of the array first element N + second element N-1 + … penultimate element 2 + last element 1 Total N(N+1)/2 = (N 2 +N)/2 http://ecomputernotes.com
  • 11. Insertion Sort Basic idea ( sorting cards ): Starts by considering the first two elements of the array data, if out of order, swap them Consider the third element, insert it into the proper position among the first three elements. Consider the forth element, insert it into the proper position among the first four elements. … … http://ecomputernotes.com
  • 12. Insertion Sort -- Example 1 2 3 0 12 5 7 19 a : 1 2 3 0 19 5 7 12 a : 1 2 3 0 12 19 7 5 a : 1 2 3 0 7 12 19 5 a : http://ecomputernotes.com
  • 13. Insertion Sort: Code void insertionSort(int *arr, int N) { int pos, count, val; for(count=1; count < N; count++) { val = arr[count]; for(pos=count-1; pos >= 0; pos--) if (arr[pos] > val) arr[pos+1]=arr[pos]; else break; arr[pos+1] = val; } } http://ecomputernotes.com
  • 14. Insertion Sort -- animation 1 2 3 0 12 5 7 19 a : 1 2 3 0 19 5 7 19 a : count val pos 1 12 0 1 12 -1 1 2 3 0 5 7 19 a : 12 19 1 2 3 0 19 5 7 12 a : 12 http://ecomputernotes.com
  • 15. Insertion Sort -- animation (cont) 1 2 3 0 12 19 7 5 a : count val pos 2 5 1 2 5 -1 1 2 3 0 19 5 7 12 a : 1 2 3 0 5 7 12 a : 19 19 1 2 3 0 19 7 12 a : 19 12 2 5 0 1 2 3 0 12 19 7 12 a : 5 http://ecomputernotes.com
  • 16. Insertion Sort -- animation (cont) 1 2 3 0 12 19 7 5 a : count val pos 3 7 2 3 7 0 3 7 1 1 2 3 0 12 19 7 5 a : 19 1 2 3 0 12 19 19 5 a : 12 1 2 3 0 12 12 19 5 a : 7 1 2 3 0 7 12 19 5 a : http://ecomputernotes.com
  • 17. Insertion Sort Analysis What is the time complexity of this algorithm? Worst case > Average case > Best case Each iteration inserts an element at the start of the array, shifting all sorted elements along second element 2 + … penultimate element N-1 + last element N Total (2+N)(N-1)/2 = O(N 2 ) http://ecomputernotes.com
  • 18. Bubble Sort Basic idea ( lighter bubbles rise to the top ): Exchange neighbouring items until the largest item reaches the end of the array Repeat for the rest of the array http://ecomputernotes.com
  • 19. Bubble Sort -- Example 1 2 3 0 12 7 19 5 a: 1 2 3 0 12 7 19 5 a: 19 12 7 5 1 2 3 0 a: 12 19 7 5 a: 1 2 3 0 5 12 7 19 1 2 3 0 a: a: 1 2 3 0 12 7 19 5 a: 1 2 3 0 7 12 19 5 a: 1 2 3 0 7 12 19 5 a: 1 2 3 0 7 12 19 5 a: 1 2 3 0 7 12 19 5 a: http://ecomputernotes.com
  • 20. Bubble Sort: Code void bubbleSort(int *arr, int N){ int i, temp, bound = N-1; int swapped = 1; while (swapped > 0 ) { swapped = 0; for(i=0; I < bound; i++) if ( arr[i] > arr[i+1] ) { temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; swapped = i; } bound = swapped; } } http://ecomputernotes.com
  • 21. Bubble Sort Analysis What is the time complexity of this algorithm? Worst case > Average case > Best case Each iteration compares all the adjacent elements, swapping them if necessary first iteration N + second iteration N-1 + … last iteration 1 Total N(1+N)/2 = O(N 2 ) http://ecomputernotes.com
  • 22. Summary Insertion, Selection and Bubble sort: Worst case time complexity is proportional to N 2 . Best sorting routines are N log(N) http://ecomputernotes.com
  • 23. NLogN Algorithms Divide and Conquer Merge Sort Quick Sort Heap Sort http://ecomputernotes.com
  • 24. Divide and Conquer What if we split the list into two parts? 10 12 8 4 2 11 7 5 10 12 8 4 2 11 7 5 http://ecomputernotes.com
  • 25. Divide and Conquer Sort the two parts: 10 12 8 4 2 11 7 5 4 8 10 12 2 5 7 11 http://ecomputernotes.com
  • 26. Divide and Conquer Then merge the two parts together: 4 8 10 12 2 5 7 11 2 4 5 7 8 10 11 12 http://ecomputernotes.com

Editor's Notes

  • #6: End of lecture 43. Start of 44
  • #27: End of lecture 44.